package com.ruoyi.tongji.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.beetl.sql.core.SQLReady;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class DateUtil {
    
    public static Date MAX_DATE = maxDate();

    public static String getJiduFirstDay(Date date, String pattern) {
        LocalDate currentDate = LocalDate.now();
        Month currentMonth = currentDate.getMonth();
        int currentYear = currentDate.getYear();

        LocalDate firstDay, lastDay;

        if (currentMonth == Month.JANUARY || currentMonth == Month.FEBRUARY || currentMonth == Month.MARCH) {
            firstDay = LocalDate.of(currentYear, Month.JANUARY, 1);
            lastDay = LocalDate.of(currentYear, Month.MARCH, 31);
        } else if (currentMonth == Month.APRIL || currentMonth == Month.MAY || currentMonth == Month.JUNE) {
            firstDay = LocalDate.of(currentYear, Month.APRIL, 1);
            lastDay = LocalDate.of(currentYear, Month.JUNE, 30);
        } else if (currentMonth == Month.JULY || currentMonth == Month.AUGUST || currentMonth == Month.SEPTEMBER) {
            firstDay = LocalDate.of(currentYear, Month.JULY, 1);
            lastDay = LocalDate.of(currentYear, Month.SEPTEMBER, 30);
        } else {
            firstDay = LocalDate.of(currentYear, Month.OCTOBER, 1);
            lastDay = LocalDate.of(currentYear, Month.DECEMBER, 31);
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return firstDay.format(formatter);
    }
    public static String getJiduLastDay(Date date, String pattern) {
        LocalDate currentDate = LocalDate.now();
        Month currentMonth = currentDate.getMonth();
        int currentYear = currentDate.getYear();

        LocalDate firstDay, lastDay;

        if (currentMonth == Month.JANUARY || currentMonth == Month.FEBRUARY || currentMonth == Month.MARCH) {
            firstDay = LocalDate.of(currentYear, Month.JANUARY, 1);
            lastDay = LocalDate.of(currentYear, Month.MARCH, 31);
        } else if (currentMonth == Month.APRIL || currentMonth == Month.MAY || currentMonth == Month.JUNE) {
            firstDay = LocalDate.of(currentYear, Month.APRIL, 1);
            lastDay = LocalDate.of(currentYear, Month.JUNE, 30);
        } else if (currentMonth == Month.JULY || currentMonth == Month.AUGUST || currentMonth == Month.SEPTEMBER) {
            firstDay = LocalDate.of(currentYear, Month.JULY, 1);
            lastDay = LocalDate.of(currentYear, Month.SEPTEMBER, 30);
        } else {
            firstDay = LocalDate.of(currentYear, Month.OCTOBER, 1);
            lastDay = LocalDate.of(currentYear, Month.DECEMBER, 31);
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return lastDay.format(formatter);
    }
    public static int getJidu(Date date) {
        Calendar calendar = Calendar.getInstance();
        int quarter = (calendar.get(Calendar.MONTH) / 3) + 1;
        return quarter;
    }
    public static String getLastYearFirstDay(Date date, String pattern) {
        YearMonth ym = YearMonth.of(NumberUtils.toInt(getYear(date))-1,1);
        LocalDate lastDate = ym.atDay(1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return lastDate.format(formatter);
    }
    public static String getLastYearLastDay(Date date, String pattern) {
        LocalDate today = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate lastDayOfLastYear = today.withDayOfYear(1).minusDays(1);
        //这段代码首先获取了当前日期，然后使用withDayOfYear(1)将当前日期调整到当年的第一天，接着使用minusDays(1)来回退一天，以获取上一年的最后一天。
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return lastDayOfLastYear.format(formatter);
    }
    public static String getYearFirstDay(Date date, String pattern) {
        YearMonth ym = YearMonth.of(NumberUtils.toInt(getYear(date)),1);
        LocalDate lastDate = ym.atDay(1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return lastDate.format(formatter);
    }
    public static String getYearLastDay(Date date, String pattern) {
        // 获取当前年份
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);

        // 创建Calendar对象并设置为下一年的第一天
        calendar.set(year + 1, 0, 1);

        // 往前倒退一天，获取年最后一天
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(calendar.getTime());
    }
    public static String getMonthFirstDay(Date date, String pattern) {
        YearMonth ym = YearMonth.of(NumberUtils.toInt(getYear(date)),NumberUtils.toInt(getMonth(date)));
        LocalDate lastDate = ym.atDay(1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return lastDate.format(formatter);
    }
    public static String getMonthLastDay(Date date, String pattern) {
        YearMonth ym = YearMonth.of(NumberUtils.toInt(getYear(date)),NumberUtils.toInt(getMonth(date)));
        LocalDate lastDate = ym.atEndOfMonth();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return lastDate.format(formatter);
    }
    public static String getMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int month = cal.get(Calendar.MONTH) + 1;

        return String.valueOf(month);
    }

    public static String getYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(Calendar.YEAR);

        return String.valueOf(year);
    }
    
    public static String now() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date());
    }
    
    public static String now(String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date());
    }
    
    private static Date maxDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse("9999-12-31 23:59:59");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    public static String format(Date date, String pattern) {
        if (pattern == null) {
            pattern = "yyyy-MM-dd";
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }
    public static Date getDate(String strDate) {
		if (StringUtils.isBlank(strDate)) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
        format.setLenient(false);
        try {
            return format.parse(strDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println("com.sunzhe.util.DateUtils getDate() ： 时间格式不正确，应为yyyy-MM-dd");
			return null;
		}
	}

    public static Date getDate(String strDate, String pattern) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
        format.setLenient(false);
        try {
            return format.parse(strDate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
            return null;
        }
    }

    public static Date getDate(String strDate, String pattern, Integer days) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
        format.setLenient(false);
        try {

            Date date = format.parse(strDate);

            Calendar cal = Calendar.getInstance();
            cal.setTime(date);//设置起时间
            cal.add(Calendar.DATE, days);//增加一天
            return cal.getTime();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
            return null;
        }
    }

    public static int getMondayCount(LocalDate localDate){
        if (localDate == null) {
            localDate = LocalDate.now();
        }
        LocalDate monthFirstDate = localDate.withDayOfMonth(1);
        LocalDate monthFixFirstDate = monthFirstDate;

        LocalDate monthLastDate = monthFirstDate.plusMonths(1);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); // 设置日期格式为"年-月-日"

        List dayReportList = new ArrayList();
        YearMonth yearMonth = YearMonth.of(monthFirstDate.getYear(),monthFirstDate.getMonth());

        int mondayCount = 0;
        while (monthFirstDate.isBefore(monthLastDate)) {
            LocalDate date = monthFirstDate;
            DayOfWeek dayOfWeek = date.getDayOfWeek();

            monthFirstDate = monthFirstDate.plusDays(1);

            if (com.ruoyi.common.utils.StringUtils.equals(dayOfWeek.name(),"MONDAY")) {
                mondayCount = mondayCount + 1;
            }
        }
        return mondayCount;
    }

    public static void main(String[] args) {
        File dir = new File("E:\\桌面\\重度残疾、精神疾病名单人员最后一次献浆信息");
        File[] files = dir.listFiles();

        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            String fileName = file.getName();
            String idCard = fileName.split("-")[2];
            //System.out.print("\"" + idCard + "\",");
            System.out.println(fileName);
        }
    }
}
